package com.gxc.webcore.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.gxc.webcore.util.ContextUtil;
import com.gxc.webcore.util.SystemUtil;

/**
 * 系统初始化
 * 
 * @author gxc
 * 
 */
public class SystemInitServlet extends javax.servlet.http.HttpServlet {

	private static final long serialVersionUID = 1L;

	public void init() throws ServletException {

		// TODO 初始化ContextUtil
		initContextUtil();

		// TODO 初始化系统配置
		SystemUtil.init();

	}

	/**
	 * 初始化ContextUTil
	 */
	public void initContextUtil() {
		// TODO applicationContext
		ServletContext servletContext = getServletContext();
		ApplicationContext applicationContext = WebApplicationContextUtils
				.getRequiredWebApplicationContext(servletContext);
		ContextUtil.setApplicationContext(applicationContext);

		// TODO applicationRealPath
		String applicationRealPath = getServletContext().getRealPath("").trim();
		while (applicationRealPath.endsWith("/")
				|| applicationRealPath.endsWith("\\")) {
			applicationRealPath = applicationRealPath.substring(0,
					applicationRealPath.length() - 1);
		}
		ContextUtil.setApplicationRealPath(applicationRealPath);
	}

}
